package ar.edu.unicen.exa.isistan.archassis.view.dialogs;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;

import swing2swt.layout.BorderLayout;
import ar.edu.unicen.exa.isistan.archassis.source.ISourceStrategy;
import ar.edu.unicen.exa.isistan.archassis.view.Application;
import ar.edu.unicen.exa.isistan.archassis.view.dialogs.sourcepanel.AbstractSourceStrategyPanel;
import ar.edu.unicen.exa.isistan.archassis.view.dialogs.sourcepanel.FolderSourceStrategyPanel;
import ar.edu.unicen.exa.isistan.archassis.view.dialogs.sourcepanel.SVNSourceStrategyPanel;

public class SourceDialog extends Dialog {

	protected ISourceStrategy result;
	protected Shell shell;
//	private AbstractSourceStrategyPanel panel;
	private TabFolder tabFolder;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public SourceDialog(Shell parent, int style) {
		super(parent, 0);
		setText(Application.APPLICATION_NAME + " | Datos SVN");
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public ISourceStrategy open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.MAX | SWT.RESIZE | SWT.APPLICATION_MODAL);
		shell.setSize(440, 426);
		shell.setText(getText());
		shell.setLayout(new BorderLayout(0, 0));
		
		//panel = new SVNSourceStrategyPanel(shell);
		
		
		Composite composite = new Composite(shell, SWT.NONE);
		composite.setLayoutData(BorderLayout.SOUTH);
		
		composite.setLayout(new FormLayout());
		
		Button btnAceptar = new Button(composite, SWT.NONE);
		btnAceptar.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				exitAction(true);
			}
		});
		{
			FormData formData = new FormData();
			formData.bottom = new FormAttachment(100, -5);
			formData.right = new FormAttachment(100, -5);
			btnAceptar.setLayoutData(formData);
		}
		btnAceptar.setText("Aceptar");
		
		Button btnCancelar = new Button(composite, SWT.NONE); 
		btnCancelar.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				exitAction(false);
			}
		});
		{
			FormData fd_btnCancelar = new FormData();
			fd_btnCancelar.bottom = new FormAttachment(100, -5);
			fd_btnCancelar.right = new FormAttachment(btnAceptar, -5);
			btnCancelar.setLayoutData(fd_btnCancelar);
		}
		btnCancelar.setText("Cancelar");
		
		tabFolder = new TabFolder(shell, SWT.NONE);
		tabFolder.setLayoutData(BorderLayout.CENTER);
		
		TabItem tbtmSvn = new TabItem(tabFolder, SWT.NONE);
		tbtmSvn.setText("SVN");
		
		AbstractSourceStrategyPanel composite_1 = new SVNSourceStrategyPanel(tabFolder);
		tbtmSvn.setControl(composite_1);
		
		TabItem tbtmLocal = new TabItem(tabFolder, SWT.NONE);
		tbtmLocal.setText("Local");
		
		AbstractSourceStrategyPanel composite_2 = new FolderSourceStrategyPanel(tabFolder);
//		AbstractSourceStrategyPanel panel = new FolderSourceStrategyPanel(shell);
		tbtmLocal.setControl(composite_2);
		//;composite.setLayoutData()
	}

	protected void exitAction(boolean b) {
		result = null;
		if (b) {
			TabItem selectedItem = tabFolder.getItem(tabFolder.getSelectionIndex());
			AbstractSourceStrategyPanel composite_1 = (AbstractSourceStrategyPanel)selectedItem.getControl();
			//System.out.println();
			result = composite_1.getSourceStrategy();
		}
		shell.dispose();
	}
	protected TabFolder getTabFolder() {
		return tabFolder;
	}
}
